package com.tanhua.dubbo.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.api.db.SettingApi;
import com.tanhua.dubbo.api.mapper.QuestionMapper;
import com.tanhua.dubbo.api.mapper.SettingsMapper;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import com.tanhua.model.vo.QuestionVo;
import com.tanhua.model.vo.SettingsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Date;

@DubboService
@Slf4j
public class QuestionApiImpl implements SettingApi {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     *      设置陌生人问题
     * @param questionVo
     */
    @Override
    public void setQuestion(QuestionVo questionVo) {
        LambdaQueryWrapper<Question> qw = new LambdaQueryWrapper<>();
        qw.eq(Question::getUserId,questionVo.getUserId());
        Question question1 = questionMapper.selectOne(qw);
        Question question = new Question();
        question.setTxt(questionVo.getContent());
        question.setUserId(questionVo.getUserId());
        if (question1 == null) {
            questionMapper.insert(question);
        }else {
            LambdaQueryWrapper<Question> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(Question::getUserId,questionVo.getUserId());
            questionMapper.update(question,qw1);
        }

    }

    /**
     *      设置推送设置
     * @param settingsVo
     */
    @Override
    public void setNotification(SettingsVo settingsVo) {
        LambdaQueryWrapper<Settings> qw = new LambdaQueryWrapper<>();
        qw.eq(Settings::getUserId,settingsVo.getId());
        Settings settings = new Settings();
        settings.setLikeNotification(settingsVo.getLikeNotification());
        settings.setPinglunNotification(settingsVo.getPinglunNotification());
        settings.setGonggaoNotification(settingsVo.getGonggaoNotification());
        log.info("============>"+settings.toString());
        settingsMapper.update(settings,qw);
    }

    /**
     *      获取陌生人问题
     * @param userId
     * @return
     */
    @Override
    public String getQuestions(Long userId) {
        LambdaQueryWrapper<Question> qw = new LambdaQueryWrapper<>();
        qw.eq(Question::getUserId,userId);
        Question question = questionMapper.selectOne(qw);
        if (question == null){
            return "对方未设置问题，跟他(她)打个招呼吧";
        }
        return question.getTxt();

    }

    /**
     *      获取推送设置
     * @param userId
     * @return
     */
    @Override
    public Settings getNotification(Long userId) {
        LambdaQueryWrapper<Settings> qw = new LambdaQueryWrapper<>();
        qw.eq(Settings::getUserId,userId);
        Settings settings = settingsMapper.selectOne(qw);
        if (BeanUtil.isEmpty(settings)){
            settings.setLikeNotification(true);
            settings.setGonggaoNotification(true);
            settings.setPinglunNotification(true);
            settings.setUserId(userId);
            settingsMapper.insert(settings);
        }
        return settings;
    }
}
